﻿//###HEADER###
//Title: RevitPlus
//Description of Program: Utilzing Revit geometry and properties, user is able to perform an EnergyPlus calculation within Revit.
//Class:BatchFiler
//Description of Class: Contains functions, which define the values to be written to the idf in the Class: IDFcompiler    
//       
//By: Steve Chick & Alexander Bernstein
//Date: 12/18/2012
//Version: 001

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Revit_Addin1
{
    // Class contains functions, which define the values to be written to the idf in the Class: IDFcompiler
    public class BatchFiler
    {

        //Heading created for IDF
        public void IDFInfo(String idffile, out String newidffile)
        {

            idffile = idffile.Replace("!-Intro", "!-Generated by RevitPlus V 0000.01" + Environment.NewLine + "!-Intro"); // Standard Information
            idffile = idffile.Replace("!-Intro", "!-Created on " + System.DateTime.Now.ToString() + Environment.NewLine + "!-Intro"); // Standard INformation

            newidffile = idffile;

        }


        // Run Period defined
        public void RunPeriod(string idffile, out string newidffile)
        {
            idffile = idffile.Replace("!-RunPeriod", "RunPeriod," + Environment.NewLine + "    !-RunPeriod"); //Heading
            idffile = idffile.Replace("!-RunPeriod", ",\t\t\t!- Name" + Environment.NewLine + "    !-RunPeriod"); // Name of Run Period
            idffile = idffile.Replace("!-RunPeriod", StoredVariables.StMonth + ",\t\t\t!- Begin Month" + Environment.NewLine + "    !-RunPeriod"); // Starting Month (user input)
            idffile = idffile.Replace("!-RunPeriod", StoredVariables.StDay + ",\t\t\t!- Begin Day" + Environment.NewLine + "    !-RunPeriod"); // Starting Day (user input)
            idffile = idffile.Replace("!-RunPeriod", StoredVariables.EndMonth + ",\t\t\t!- End Month" + Environment.NewLine + "    !-RunPeriod"); // Ending Month (user input)
            idffile = idffile.Replace("!-RunPeriod", StoredVariables.EndDay + ",\t\t\t!- End Day" + Environment.NewLine + "    !-RunPeriod");// Ending Day (user input)
            idffile = idffile.Replace("!-RunPeriod", "UseWeatherFile" + ",\t\t\t!- Day of Week for Start Day" + Environment.NewLine + "    !-RunPeriod");// Day of Week to Start 
            idffile = idffile.Replace("!-RunPeriod", "Yes" + ",\t\t\t!- Use Weather File Holidays and Special Days" + Environment.NewLine + "    !-RunPeriod");// Standard
            idffile = idffile.Replace("!-RunPeriod", "No" + ",\t\t\t!- Use Weather File Daylight Saving Period" + Environment.NewLine + "    !-RunPeriod");// Standard
            idffile = idffile.Replace("!-RunPeriod", "Yes" + ",\t\t\t!- Apply weekend holiday rule" + Environment.NewLine + "    !-RunPeriod");// Standard
            idffile = idffile.Replace("!-RunPeriod", "Yes" + ",\t\t\t!- Use Weather File Rain Indicators" + Environment.NewLine + "    !-RunPeriod");// Standard
            idffile = idffile.Replace("!-RunPeriod", "Yes" + ",\t\t\t!- Use Weather File Snow Indicators" + Environment.NewLine + "    !-RunPeriod");// Standard
            idffile = idffile.Replace("!-RunPeriod", "1" + ";\t\t\t!- Number of Times Runperiod to be Repeated" + Environment.NewLine + "    !-RunPeriod");// Standard

            newidffile = idffile;
        }
        
        //Writes building information to the idf
        public void BuildingInfo(String idffile, out String newidffile)
        {

            idffile = idffile.Replace("!-Building Info", "Building," + Environment.NewLine + "    !-Building Info"); // Heading
            idffile = idffile.Replace("!-Building Info", StoredVariables.ProjectName + ",\t\t\t!- Name" + Environment.NewLine + "    !-Building Info");// Name
            idffile = idffile.Replace("!-Building Info", StoredVariables.NorthAxis + ",\t\t\t!- North Axis {deg}" + Environment.NewLine + "    !-Building Info");// North Axis (user input)
            idffile = idffile.Replace("!-Building Info", StoredVariables.Terrain + ",\t\t\t!- Terrain" + Environment.NewLine + "    !-Building Info");// Terrain (ie. subburb, rural ect) (user input)
            idffile = idffile.Replace("!-Building Info", StoredVariables.LoadTolerance + ",\t\t\t!- Loads Convergence Tolerance Value" + Environment.NewLine + "    !-Building Info");// Load Tolerance (user input)
            idffile = idffile.Replace("!-Building Info", StoredVariables.TempTolerance + ",\t\t\t!- Temperature Convergence Tolerance Value {deltaC}" + Environment.NewLine + "    !-Building Info"); //Temp Convergence (user input)
            idffile = idffile.Replace("!-Building Info", StoredVariables.SolarDistribution + ",\t\t\t!- Solar Distribution" + Environment.NewLine + "    !-Building Info"); // Solar Distribution (user input)
            idffile = idffile.Replace("!-Building Info", StoredVariables.WarmpupMax + ",\t\t\t!- Maximum Number of Warmup Days" + Environment.NewLine + "    !-Building Info"); // Max number of warm up days (user input) * Less that 25
            idffile = idffile.Replace("!-Building Info", StoredVariables.WarmpupMin + ";\t\t\t!- Minimum Number of Warmup Days" + Environment.NewLine + "    !-Building Info"); // Min number of warm up days (user input) * Greater than 6

            newidffile = idffile;
        }

        // Replaces TimeStep
        public void TimeStep(String idffile, out String newidffile) 
        {
            //Timestep
            idffile = idffile.Replace("!-TimeStep", "TimeStep" + "," + StoredVariables.TimeStepHrs + ";" + Environment.NewLine + "    !-TimeStep");//  Standard value used (4) 

            newidffile = idffile;
        }

        


        //Defines the surface construction values to be written to the idf and to be used by SurfaceProperties function in this class 
        public void Construction(String idffile, out String newidffile)
        {
           //WALL
            //For each wall, a construction is needed, therefore a for loop is utilized. One material is utilized for each construction.
            for(int i = 0; i <= StoredVariables.WallTypes.Count-1; i++)
            {
                idffile = idffile.Replace("!-ConstructionWall", "Construction," + Environment.NewLine + "    !-ConstructionWall"); // Construction
                idffile = idffile.Replace("!-ConstructionWall", "Wall" + i.ToString() + ",\t\t\t!- Name" + Environment.NewLine + "    !-ConstructionWall"); // Name
                idffile = idffile.Replace("!-ConstructionWall", StoredVariables.WallTypes[i] + ";\t\t\t!- Outside Layer" + Environment.NewLine + Environment.NewLine + "    !-ConstructionWall"); // WallType
            }

           //FLOOR
            //For each floor, a construction is needed, therefore a for loop is utilized. One material is utilized for each construction.
            for (int i = 0; i <= StoredVariables.FloorTypes.Count - 1; i++)
            {
                idffile = idffile.Replace("!-ConstructionFloor", "Construction," + Environment.NewLine + "    !-ConstructionFloor"); // Construction
                idffile = idffile.Replace("!-ConstructionFloor", "Floor" + i.ToString() + ",\t\t\t!- Name" + Environment.NewLine + "    !-ConstructionFloor"); // Name
                idffile = idffile.Replace("!-ConstructionFloor", StoredVariables.FloorTypes[i] + ";\t\t\t!- OutsideLayer" + Environment.NewLine + Environment.NewLine + "    !-ConstructionFloor"); //FloorType      
            }

           //ROOF
            //For each roof, a construction is needed, therefore a for loop is utilized. One material is utilized for each construction.
            for(int i = 0; i <= StoredVariables.RoofTypes.Count-1; i++)
            {
                idffile = idffile.Replace("!-ConstructionRoof", "Construction," + Environment.NewLine + "    !-ConstructionRoof"); // Construction
                idffile = idffile.Replace("!-ConstructionRoof", "Roof" + i.ToString() + ",\t\t\t!- Name" + Environment.NewLine + "    !-ConstructionRoof"); // Name
                idffile = idffile.Replace("!-ConstructionRoof", StoredVariables.RoofTypes[i] + ";\t\t\t!- Outside Layer" + Environment.NewLine + Environment.NewLine +  "    !-ConstructionRoof"); // RoofType
            }


            newidffile = idffile;
        }

        //Defines surface material properties to be written to the idf and to be utilized in the Construction function. 
        public void Materials(String idffile, out String newidffile)
        {
           //Wall - Each wall has one material (material no mass), therefore a for loop is utilized. The material is chosen by the user
            for (int i = 0; i <= StoredVariables.WallTypes.Count-1; i++)
            {

                idffile = idffile.Replace("!-Material", "Material:NoMass," + Environment.NewLine + "    !-Material");
                idffile = idffile.Replace("!-Material", StoredVariables.WallTypes[i] + " " + ",\t\t\t!- Name" + Environment.NewLine + "    !-Material"); //Name
                idffile = idffile.Replace("!-Material", StoredVariables.WallRoughness[i] + ",\t\t\t!- Roughness" + Environment.NewLine + "    !-Material"); //Roughness 
                idffile = idffile.Replace("!-Material", StoredVariables.WallThermalResistance[i] + ",\t\t\t!- Thickness (m)" + Environment.NewLine + "    !-Material"); //Thermal Resistance 
                idffile = idffile.Replace("!-Material", StoredVariables.WallThermalAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Thermal Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.WallSolarAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Solar Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.WallVisibleAbsorptance[i] + ";\t\t\t!- Thermal Absorptance" + Environment.NewLine + " " + Environment.NewLine + "    !-Material"); //Visible Absorptance 
            }

            //Floor - Each floor has one material (material no mass), therefore a for loop is utilized. The material is chosen by the user
            for (int i = 0; i <= StoredVariables.FloorTypes.Count - 1; i++)
            {

                idffile = idffile.Replace("!-Material", "Material:NoMass," + Environment.NewLine + "    !-Material");
                idffile = idffile.Replace("!-Material", StoredVariables.FloorTypes[i] + " " + ",\t\t\t!- Name" + Environment.NewLine + "    !-Material"); //Name
                idffile = idffile.Replace("!-Material", StoredVariables.FloorRoughness[i] + ",\t\t\t!- Roughness" + Environment.NewLine + "    !-Material"); //Roughness 
                idffile = idffile.Replace("!-Material", StoredVariables.FloorThermalResistance[i] + ",\t\t\t!- Thickness (m)" + Environment.NewLine + "    !-Material"); //Thermal Resistance 
                idffile = idffile.Replace("!-Material", StoredVariables.FloorThermalAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Thermal Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.FloorSolarAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Solar Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.FloorVisibleAbsorptance[i] + ";\t\t\t!- Thermal Absorptance" + Environment.NewLine + " " + Environment.NewLine + "    !-Material"); //Visible Absorptance 
            }

            //Roof - Each roof has one material (material no mass), therefore a for loop is utilized. The material is chosen by the user
            for (int i = 0; i <= StoredVariables.RoofTypes.Count - 1; i++)
            {

                idffile = idffile.Replace("!-Material", "Material:NoMass," + Environment.NewLine + "    !-Material");
                idffile = idffile.Replace("!-Material", StoredVariables.RoofTypes[i] + " "  + ",\t\t\t!- Name" + Environment.NewLine + "    !-Material"); //Name
                idffile = idffile.Replace("!-Material", StoredVariables.RoofRoughness[i] + ",\t\t\t!- Roughness" + Environment.NewLine + "    !-Material"); //Roughness 
                idffile = idffile.Replace("!-Material", StoredVariables.RoofThermalResistance[i] + ",\t\t\t!- Thickness (m)" + Environment.NewLine + "    !-Material"); //Thermal Resistance 
                idffile = idffile.Replace("!-Material", StoredVariables.RoofThermalAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Thermal Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.RoofSolarAbsorptance[i] + ",\t\t\t!- Thermal Resistance" + Environment.NewLine + "    !-Material"); //Solar Absorptance
                idffile = idffile.Replace("!-Material", StoredVariables.RoofVisibleAbsorptance[i] + ";\t\t\t!- Thermal Absorptance" + Environment.NewLine + " " + Environment.NewLine + "    !-Material"); //Visible Absorptance 
            }
            
            newidffile = idffile;
        }



        // Defines zone information values to be written to the idf and used in other functions.
        public void Zones(String idffile, out String newidffile) 
        {
            //For Each Zone (Revit room), an zone Name and description is given below. Most values are considered standard 
            foreach (string zone in StoredVariables.Roomnames)
            {
                idffile = idffile.Replace("!-Zones", " Zone, " + Environment.NewLine + "    !-Zones"); // Heading "Zone"
                idffile = idffile.Replace("!-Zones", zone + ",\t\t\t!- Name" + Environment.NewLine + "    !-Zones"); // Zone number, i
                idffile = idffile.Replace("!-Zones", "0" + ",\t\t\t!- Direction of Relative North {deg}" + Environment.NewLine + "    !-Zones"); // Standard
                idffile = idffile.Replace("!-Zones", "0" + ",\t\t\t!- X Origina {m}" + Environment.NewLine + "    !-Zones"); // Standard
                idffile = idffile.Replace("!-Zones", "0" + ",\t\t\t!- Y Origina {m}" + Environment.NewLine + "    !-Zones"); //Standard
                idffile = idffile.Replace("!-Zones", "0" + ",\t\t\t!- Z Origina {m}" + Environment.NewLine + "    !-Zones"); //Standard
                idffile = idffile.Replace("!-Zones", "1" + ",\t\t\t!- Type" + Environment.NewLine + "    !-Zones"); //Standard
                idffile = idffile.Replace("!-Zones", "1" + ",\t\t\t!- Multiplier" + Environment.NewLine + "    !-Zones"); //Standard
                idffile = idffile.Replace("!-Zones", "autocalculate" + ",\t\t\t!- Ceiling Height {m}" + Environment.NewLine + "    !-Zones"); //Standard
                idffile = idffile.Replace("!-Zones", "autocalculate" + ";\t\t\t!- Volume {m3}" + Environment.NewLine + Environment.NewLine + "    !-Zones"); // Standard

            }
            
            newidffile = idffile;
        }
        // Defines all wall/floor/roof information (name, zone, location, material, construction, ect) to be written to the idf in IDFcompiler.
        public void Surface(String idffile, out String newidffile)
        {

            int Zone = 0; // Initializes for count
            int BoundSeg = 0; // Initializes for count

          //******WALL********
            foreach (List<List<string>> BoundarySegments in StoredVariables.WallBoundary) // Accessing the list of boundary segment(list of points within)
            {
                int BoundSeg2 = 0; // Intitializes for count
               foreach (List<string> WallPts in BoundarySegments) // Acceses each Point stored in the list of boundary segments.
                {
                    // Alters input string (x,y,z) to string for idf x,y,z and creates variable to be used below
                    string newptfmt1 = StoredVariables.WallBoundary[Zone][BoundSeg2][0].Replace(")", "");
                    newptfmt1 = newptfmt1.Replace("(", "");
                    string newptfmt2 = StoredVariables.WallBoundary[Zone][BoundSeg2][1].Replace(")", "");
                    newptfmt2 = newptfmt2.Replace("(", "");
                    string newptfmt3 = StoredVariables.WallBoundary[Zone][BoundSeg2][2].Replace(")", "");
                    newptfmt3 = newptfmt3.Replace("(", "");
                    string newptfmt4 = StoredVariables.WallBoundary[Zone][BoundSeg2][3].Replace(")", "");
                    newptfmt4 = newptfmt4.Replace("(", "");
                        
                            // replaces key word "SurfaceDetailWall" with strings in the idf
                            idffile = idffile.Replace("!-SurfaceDetailWall", "BuildingSurface:Detailed," + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard
                            idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ":" + "Wall" + BoundSeg.ToString() + " " + ",\t\t\t!- Name" + Environment.NewLine + "    !-SurfaceDetailWall");//Name 
                            idffile = idffile.Replace("!-SurfaceDetailWall", "Wall" + ",\t\t\t!- Surface Type" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard
                            idffile = idffile.Replace("!-SurfaceDetailWall", "Wall"+StoredVariables.WallIndex[BoundSeg].ToString() + ",\t\t\t!- Construction Name (m)" + Environment.NewLine + "    !-SurfaceDetailWall"); // Uses same construction name as defined above in Construction function
                            idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ",\t\t\t!- Zone Name" + Environment.NewLine + "    !-SurfaceDetailWall"); // Name of Zone that wall is a part of
                            idffile = idffile.Replace("!-SurfaceDetailWall", "Outdoors" + ",\t\t\t!- Outside Boundary Condition" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard
                            idffile = idffile.Replace("!-SurfaceDetailWall", ",\t\t\t!- Outside Boundary Condition Object" + Environment.NewLine + "    !-SurfaceDetailWall"); //Standard
                            
                            string sun = "SunExposed";
                            string wind = "WindExposed";

                            //If statement changes sun & wind variable if wall is an interior wall(has an index of 0)
                            if (StoredVariables.WallFunction[StoredVariables.WallIndex[BoundSeg]] == 0)
                                {
                                    sun = "NoSun";
                                    wind = "NoWind";
                                }
                           
                            idffile = idffile.Replace("!-SurfaceDetailWall", sun + ",\t\t\t!- Sun Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // Interior or Exterior Wall
                            idffile = idffile.Replace("!-SurfaceDetailWall", wind + ",\t\t\t!- Wind Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // Interior or Exterior Wall
                            idffile = idffile.Replace("!-SurfaceDetailWall", "0.50000000" + ",\t\t\t!- View Factor to Ground" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard

                            idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.WallBoundary[Zone][BoundSeg2].Count + ",\t\t\t!- Number of Vertices" + Environment.NewLine + "    !-SurfaceDetailWall"); // Point count (always 4)
                            idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt1 + ",\t\t\t!- X,Y,Z ==> Vertex 1 {m}" + Environment.NewLine + "    !-SurfaceDetailWall"); // Point1 in x,y,z string
                            idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt2 + ",\t\t\t!- X,Y,Z ==> Vertex 2 {m}" + Environment.NewLine + "    !-SurfaceDetailWall"); // Point2 in x,y,z string
                            idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt3 + ",\t\t\t!- X,Y,Z ==> Vertex 3 {m}" + Environment.NewLine + "    !-SurfaceDetailWall"); // Point3 in x,y,z string
                            idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt4 + ";\t\t\t!- X,Y,Z ==> Vertex 4 {m}" + Environment.NewLine + " " + Environment.NewLine + "    !-SurfaceDetailWall"); //Point4 in x,y,z string
                    
                   BoundSeg+=1;
                }
                Zone+=1;
            }

          //******Floor********  
            Zone = 0;// Initializes for count

            foreach (List<string> Zones in StoredVariables.FloorBoundary) // Each Zone has 1 floor, therefore utlizing the list of Zones, the floors can be found
            {

                    idffile = idffile.Replace("!-SurfaceDetailWall", "BuildingSurface:Detailed," + Environment.NewLine + "    !-SurfaceDetailWall");// Standard
                    idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ":" + "Floor" + Zone.ToString() + " " + ",\t\t\t!- Name" + Environment.NewLine + "    !-SurfaceDetailWall");//Name
                    idffile = idffile.Replace("!-SurfaceDetailWall", "Floor" + ",\t\t\t!- Surface Type" + Environment.NewLine + "    !-SurfaceDetailWall"); //Standard
                    idffile = idffile.Replace("!-SurfaceDetailWall", "Floor" + StoredVariables.FloorIndex[Zone] + ",\t\t\t!- Construction Name (m)" + Environment.NewLine + "    !-SurfaceDetailWall"); //Use same construction name as defined above in Construction function
                    idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ",\t\t\t!- Zone Name" + Environment.NewLine + "    !-SurfaceDetailWall"); //Name of Zone that floor is part of
                    idffile = idffile.Replace("!-SurfaceDetailWall", "Ground" + ",\t\t\t!- Outside Boundary Condition" + Environment.NewLine + "    !-SurfaceDetailWall"); //Standard
                    idffile = idffile.Replace("!-SurfaceDetailWall", ",\t\t\t!- Outside Boundary Condition Object" + Environment.NewLine + "    !-SurfaceDetailWall"); //Standard


                    idffile = idffile.Replace("!-SurfaceDetailWall", "NoSun" + ",\t\t\t!- Sun Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // All floor are considered interior
                    idffile = idffile.Replace("!-SurfaceDetailWall", "NoWind" + ",\t\t\t!- Wind Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // All floors are considered interior
                    idffile = idffile.Replace("!-SurfaceDetailWall", "0.50000000" + ",\t\t\t!- View Factor to Ground" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard value
                    idffile = idffile.Replace("!-SurfaceDetailWall", Zones.Count + ",\t\t\t!- Number of Vertices" + Environment.NewLine + "    !-SurfaceDetailWall"); //Point count (can vary)
                    

                //Point count can vary, therefore a for loop is neccesary to write points
                int FloorPoint = 0; // Initializes for count
                foreach (string FloorPt in Zones)
                {

                    int j = FloorPoint + 1;
                    string k = j.ToString();// Makes floor # a string to write to idf

                    //The last point must have a ";" instead of a "," in order for the idf to work, therefore an if statement is neccesary
                    if (FloorPoint < Zones.Count-1)
                    {
                        // Alters input string (x,y,z) to string for idf x,y,z and creates variable to be used below
                        string newptfmt = FloorPt.Replace(")", "");
                        newptfmt = newptfmt.Replace("(", "");

                        idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt + ",\t\t\t!- X,Y,Z ==> Vertex" + k + "{m}" + Environment.NewLine + " " + Environment.NewLine + "    !-SurfaceDetailWall"); //
                    }
                    else
                    {
                        // Changes revit point (x,y,z) to string for idf x,y,z and creates variable to be used below
                        string newptfmt = FloorPt.Replace(")", "");
                        newptfmt = newptfmt.Replace("(", "");
                        
                        idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt + ";\t\t\t!- X,Y,Z ==> Vertex" + k + "{m}" + Environment.NewLine + " " + Environment.NewLine + "    !-SurfaceDetailWall"); //
                    }

                    FloorPoint += 1;
                }
                Zone += 1;
            }

            //********Roof*******
            Zone = 0;// Initializes for count
            foreach (List<string> Zones in StoredVariables.RoofBoundary) // Each Zone has 1 roof, therefore utlizing the list of Zones, the floors can be found
            {

                idffile = idffile.Replace("!-SurfaceDetailWall", "BuildingSurface:Detailed," + Environment.NewLine + "    !-SurfaceDetailWall");//Standard
                idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ":" + "Roof" + Zone.ToString() + " " + ",\t\t\t!- Name" + Environment.NewLine + "    !-SurfaceDetailWall"); //Name
                idffile = idffile.Replace("!-SurfaceDetailWall", "Roof" + ",\t\t\t!- Surface Type" + Environment.NewLine + "    !-SurfaceDetailWall"); //Standard
                idffile = idffile.Replace("!-SurfaceDetailWall", "Roof" + StoredVariables.RoofIndex[Zone] + ",\t\t\t!- Construction Name (m)" + Environment.NewLine + "    !-SurfaceDetailWall"); //Use same construction name as defined above in Construction function
                idffile = idffile.Replace("!-SurfaceDetailWall", StoredVariables.Roomnames[Zone] + ",\t\t\t!- Zone Name" + Environment.NewLine + "    !-SurfaceDetailWall"); // Name of zone that the roof is a part of
                idffile = idffile.Replace("!-SurfaceDetailWall", "Outdoors" + ",\t\t\t!- Outside Boundary Condition" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard
                idffile = idffile.Replace("!-SurfaceDetailWall", ",\t\t\t!- Outside Boundary Condition Object" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard


                idffile = idffile.Replace("!-SurfaceDetailWall", "SunExposed" + ",\t\t\t!- Sun Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // All roofs are exterior
                idffile = idffile.Replace("!-SurfaceDetailWall", "WindExposed" + ",\t\t\t!- Wind Exposure" + Environment.NewLine + "    !-SurfaceDetailWall"); // All roofs are exterior
                idffile = idffile.Replace("!-SurfaceDetailWall", "0.50000000" + ",\t\t\t!- View Factor to Ground" + Environment.NewLine + "    !-SurfaceDetailWall"); // Standard
                idffile = idffile.Replace("!-SurfaceDetailWall", Zones.Count + ",\t\t\t!- Number of Vertices" + Environment.NewLine + "    !-SurfaceDetailWall"); //Point count (can vary)

                //Point count can vary, therefore a for loop is neccesary to write points
                int RoofPoint = 0; // Intializes for count 
                foreach (string RoofPt in Zones)
                {

                    int j = RoofPoint + 1;
                    string k = j.ToString(); // Makes Roof # a string to write to idf

                    //The last point must have a ";" instead of a "," in order for the idf to be read, therefore an if statement is neccesary. (IF point is not the last point "," else ";")
                    if (RoofPoint < Zones.Count-1)
                    {
                        // Alters input string (x,y,z) to string for idf x,y,z and creates variable to be used below
                        string newptfmt = RoofPt.Replace(")", "");
                        newptfmt = newptfmt.Replace("(", "");

                        idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt + ",\t\t\t!- X,Y,Z ==> Vertex" + k + "{m}" + Environment.NewLine + " " + Environment.NewLine + "    !-SurfaceDetailWall"); //Assigns points values
                    }
                    else
                    {
                        // Changes revit point (x,y,z) to string for idf x,y,z and creates variable to be used below
                        string newptfmt = RoofPt.Replace(")", "");
                        newptfmt = newptfmt.Replace("(", "");

                        idffile = idffile.Replace("!-SurfaceDetailWall", newptfmt + ";\t\t\t!- X,Y,Z ==> Vertex" + k + "{m}" + Environment.NewLine + " " + Environment.NewLine + "    !-SurfaceDetailWall"); //Assign points values
                    }

                    RoofPoint += 1;
                }
                Zone += 1;
            }

               newidffile = idffile;
        }


       //Schedules utilized for energy model.  Schedule contains information for HVAC ussage. 
        public void Schedule(String idffile, out String newidffile)
        {

            //First Schedule is Standard and neccesary to run an idf
            idffile = idffile.Replace("!-Schedules", "Schedule:Compact," + Environment.NewLine + "     !-Schedules");
            idffile = idffile.Replace("!-Schedules", "ALWAYS 4" + ",\t\t\t!- Name" + Environment.NewLine + "     !-Schedules");
            idffile = idffile.Replace("!-Schedules", "Any Number" + ",\t\t\t!- Schedule Type Limits Name" + Environment.NewLine + "     !-Schedules");
            idffile = idffile.Replace("!-Schedules", "Through: 12/31" + ",\t\t\t!- Field 1" + Environment.NewLine + "     !-Schedules");// All year has the same setpoint
            idffile = idffile.Replace("!-Schedules", "For: AllDays" + ",\t\t\t!- Field 2" + Environment.NewLine + "     !-Schedules");// Everyday has the same setpoint
            idffile = idffile.Replace("!-Schedules", "Until: 24:00,4" + ";\t\t\t!- Field 3" + Environment.NewLine + "     !-Schedules");// Same set point for 24 hours

            //Schedule is user controlled
            idffile = idffile.Replace("!-Schedules", "Schedule:Compact," + Environment.NewLine + "     !-Schedules");// Heading
            idffile = idffile.Replace("!-Schedules", "ALWAYS " + StoredVariables.HeatSetPt + ",\t\t\t!- Name" + Environment.NewLine + "     !-Schedules");// Heating temperature set point (Usually Lower of SetPoints)
            idffile = idffile.Replace("!-Schedules", "Any Number" + ",\t\t\t!- Schedule Type Limits Name" + Environment.NewLine + "     !-Schedules");
            idffile = idffile.Replace("!-Schedules", "Through: 12/31" + ",\t\t\t!- Field 1" + Environment.NewLine + "     !-Schedules");// All year has the same setpoint
            idffile = idffile.Replace("!-Schedules", "For: AllDays" + ",\t\t\t!- Field 2" + Environment.NewLine + "     !-Schedules");// Everyday has the same setpoint
            idffile = idffile.Replace("!-Schedules", "Until: 24:00," + StoredVariables.HeatSetPt + ";\t\t\t!- Field 3" + Environment.NewLine + "     !-Schedules");// Same set point for 24 hours

            //Schedule is user controlled
            idffile = idffile.Replace("!-Schedules", "Schedule:Compact," + Environment.NewLine + "     !-Schedules");// Heading
            idffile = idffile.Replace("!-Schedules", "ALWAYS " + StoredVariables.CoolSetPt + ",\t\t\t!- Name" + Environment.NewLine + "     !-Schedules");// Cooling temperature set point (Usually Higher of SetPoints)
            idffile = idffile.Replace("!-Schedules", "Any Number" + ",\t\t\t!- Schedule Type Limits Name" + Environment.NewLine + "     !-Schedules");
            idffile = idffile.Replace("!-Schedules", "Through: 12/31" + ",\t\t\t!- Field 1" + Environment.NewLine + "     !-Schedules");// All year has the same setpoint
            idffile = idffile.Replace("!-Schedules", "For: AllDays" + ",\t\t\t!- Field 2" + Environment.NewLine + "     !-Schedules");// Everyday has the same setpoint
            idffile = idffile.Replace("!-Schedules", "Until: 24:00," + StoredVariables.CoolSetPt + ";\t\t\t!- Field 3" + Environment.NewLine + "     !-Schedules");// Same set point for 24 hours


            newidffile = idffile;
        }

        
        // Defines standard HVAC Equipment connection to be written to the idf 
        public void ZoneEquipment(String idffile, out String newidffile)
        {

            //For Each Zone the idf is written to
            foreach (string zone in StoredVariables.Roomnames)
            {
                idffile = idffile.Replace("!-EquipConn", "ZoneHVAC:EquipmentConnections," + Environment.NewLine + "     !-EquipConn"); // Heading
                idffile = idffile.Replace("!-EquipConn", zone + ",\t\t\t!- Zone Name" + Environment.NewLine + "     !-EquipConn"); // Room name utilzed for idf
                idffile = idffile.Replace("!-EquipConn", zone + " Equipment" + ",\t\t\t!- Zone Conditioning Equipment List Name" + Environment.NewLine + "     !-EquipConn");// Room name utilzed for idf
                idffile = idffile.Replace("!-EquipConn", zone + " Supply Inlet" + ",\t\t\t!- Zone Air Inlet Node or Nodelist Name" + Environment.NewLine + "     !-EquipConn");// Room name utilzed for idf
                idffile = idffile.Replace("!-EquipConn", ",\t\t\t!- Zone Air Exhaust Node or Nodelist Name" + Environment.NewLine + "     !-EquipConn");
                idffile = idffile.Replace("!-EquipConn", zone + " Zone Air Node" + ",\t\t\t!- Zone Air Node Name" + Environment.NewLine + "     !-EquipConn");// Room name utilzed for idf
                idffile = idffile.Replace("!-EquipConn", zone + " Return Outlet" + ";\t\t\t!- Zone Return Air Node Name" + Environment.NewLine + Environment.NewLine + "     !-EquipConn");// Room name utilzed for idf
            }
            
            
            newidffile = idffile;
        }

        //Defines standard HVAC Equipment List to be written to the idf
        public void ZoneEquipmentList(String idffile, out String newidffile)
        {

            //For Each Zone the idf is written to
            foreach (string zone in StoredVariables.Roomnames)
            {
                idffile = idffile.Replace("!-EquipList", "ZoneHVAC:EquipmentList," + Environment.NewLine + "     !-EquipList");//Header
                idffile = idffile.Replace("!-EquipList", zone + " Equipment" + ",\t\t\t!- Name" + Environment.NewLine + "     !-EquipList");// Room name utilzed for idf
                idffile = idffile.Replace("!-EquipList", "ZoneHVAC:IdealLoadsAirSystem" + ",\t\t\t!- Schedule Type Limits Name" + Environment.NewLine + "     !-EquipList"); // Standard
                idffile = idffile.Replace("!-EquipList", zone + " Purchased Air" + ",\t\t\t!- Field 1" + Environment.NewLine + "     !-EquipList");// Room name utilzed for idf
                idffile = idffile.Replace("!-EquipList", "1" + ",\t\t\t!- Field 2" + Environment.NewLine + "     !-EquipList"); // Standard
                idffile = idffile.Replace("!-EquipList", "1" + ";\t\t\t!- Field 3" + Environment.NewLine + Environment.NewLine + "     !-EquipList"); // Standard
            }


            newidffile = idffile;
        }

        //Method of defining standard HVAC settings to be written to the idf
        public void PurchasedAir(String idffile, out String newidffile)
        {

            //For Each Zone the idf is written to
            foreach (string zone in StoredVariables.Roomnames)
            {
                idffile = idffile.Replace("!-PurchasedAir", "ZoneHVAC:IdealLoadsAirSystem," + Environment.NewLine + "     !-PurchasedAir"); // Headding
                idffile = idffile.Replace("!-PurchasedAir", zone + " Purchased Air" + ",\t\t\t!- Name" + Environment.NewLine + "     !-PurchasedAir"); //Name (utilizes Room Name)
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Availability Schedule Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", zone + " Supply Inlet" + ",\t\t\t!- Zone Supply Air Node Name" + Environment.NewLine + "     !-PurchasedAir");// Room name utilzed for idf
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Zone Exhaust Air Node Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "50" + ",\t\t\t!- Maximum Heating Supply Air Temperature {c}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "13" + ",\t\t\t!- Maximum Cooling Supply Air Temperature {c}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "0.015" + ",\t\t\t!- Maximum Heating Supply Air Humidity Ratio {kgwater/kgDryAir}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "0.01" + ",\t\t\t!- Maximum Cooling Supply Air Humidity Ratio {kgwater/kgDryAir}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "NoLimit" + ",\t\t\t!- Heating Limit" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Maximum Heating Air Flow Rate {m3/s}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Maximum Sensible HEating Capacity {W}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "NoLimit" + ",\t\t\t!- Cooling Limit" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Maximum Cooling Air Flow Rate {m3/s}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Maximum Total Cooling Capacity {W}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Heating Availability Schedule Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Cooling Availability Schedule Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "ConstantSupplyHumidityRatio" + ",\t\t\t!- Dehumidification Control Type" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Cooling Sensible Heat Ratio{dimensionless}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", "ConstantSupplyHumidityRatio" + ",\t\t\t!- Humidification Control Type" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Design Specification Outdoor Air Object Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Outdoor Air Inlet Node Name" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Demand Controlled Ventilation Type" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Outdoor Air Economizer Type" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Heat Recovery Type" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ",\t\t\t!- Sensible Heat Recovery Effectiveness {dimensionless}" + Environment.NewLine + "     !-PurchasedAir");//Standard
                idffile = idffile.Replace("!-PurchasedAir", ";\t\t\t!- Latent Heat Recovery Effectiveness {dimensionless}" + Environment.NewLine + Environment.NewLine + "     !-PurchasedAir");//Standard
            }


            newidffile = idffile;
        }



        //Applies  schedules to Zones (both defined in previous functions)
        public void ZoneControl(String idffile, out String newidffile)
        {
            //For Each Zone the idf is written to
            foreach (string zone in StoredVariables.Roomnames)
            {
                idffile = idffile.Replace("!-Zonecontrol", "ZoneControl:Thermostat," + Environment.NewLine + "     !-Zonecontrol");//Heading
                idffile = idffile.Replace("!-Zonecontrol", zone + " Thermostat" + ",\t\t\t!- Name" + Environment.NewLine + "     !-Zonecontrol"); // Utilizes room name
                idffile = idffile.Replace("!-Zonecontrol", zone + ",\t\t\t!- Zone or ZoneList Name" + Environment.NewLine + "     !-Zonecontrol");// Utilizes room name
                idffile = idffile.Replace("!-Zonecontrol", "ALWAYS 4" + ",\t\t\t!- Control Type Schedule Name" + Environment.NewLine + "     !-Zonecontrol");//Standard - Refers to schedule function
                idffile = idffile.Replace("!-Zonecontrol", "Thermostatsetpoint:Dualsetpoint" + ",\t\t\t!- Control Object Type " + Environment.NewLine + "     !-Zonecontrol"); // Standard
                idffile = idffile.Replace("!-Zonecontrol", "Office Thermostat Dual SP Control" + ";\t\t\t!- Control Name" + Environment.NewLine + Environment.NewLine + "     !-Zonecontrol");//Standard
            }
            newidffile = idffile;
        }

        //Utilizes schedules (defined above) to define setpoints
        public void DualSetPoint(String idffile, out String newidffile)
        {
            idffile = idffile.Replace("!-DualSetPoint", "ThermostatSetpoint:DualSetpoint," + Environment.NewLine + "     !-DualSetPoint");//Heading
            idffile = idffile.Replace("!-DualSetPoint", "Office Thermostat Dual SP Control" + ",\t\t\t!- Name" + Environment.NewLine + "     !-DualSetPoint"); // Standard
            idffile = idffile.Replace("!-DualSetPoint", "ALWAYS " + StoredVariables.HeatSetPt + ",\t\t\t!- Heating Setpoint Temperature Schedule Name " + Environment.NewLine + "     !-DualSetPoint");//Defines heating setpoint
            idffile = idffile.Replace("!-DualSetPoint", "ALWAYS " + StoredVariables.CoolSetPt + ";\t\t\t!- Cooling Setpoint Temperature Schedule Name" + Environment.NewLine + Environment.NewLine + "     !-DualSetPoint");//Defines cooling setpoint
            
            newidffile = idffile;
        }


        //Allows user ability to control the output data. (Daily, Weekly, Monthly, Anual) .
        public void Report(String idffile, out String newidffile)
        {

            idffile = idffile.Replace("!-OutPutVar", "Output:Variable,*,Outdoor Dry Bulb," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutVar");
            idffile = idffile.Replace("!-OutPutVar", "Output:Variable,*,Zone/Sys Sensible Cooling Energy," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutVar");
            idffile = idffile.Replace("!-OutPutVar", "Output:Variable,*,Zone/Sys Sensible Heating Energy," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutVar");
            idffile = idffile.Replace("!-OutPutVar", "Output:Variable,*,Zone/Sys Air Temperature," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutVar");

            idffile = idffile.Replace("!-OutPutMeter", "Output:Meter,DistrictHeating:Facility," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutMeter");
            idffile = idffile.Replace("!-OutPutMeter", "Output:Meter,DistrictCooling:Facility," + StoredVariables.Time + ";\t\t\t!- Report" + Environment.NewLine + Environment.NewLine + "     !-OutPutMeter");

            newidffile = idffile;
        }

       
        public void Compile()
        {

            // Creates a new directory and results folder in the RevitPlus
            String ResultsPath = @"C:\EnergyPlusV7-2-0\RevitPlus\\" + StoredVariables.ProjectName + "\\Results"; 
            System.IO.Directory.CreateDirectory(ResultsPath); // 

            // Reads BatchFile, so values can be altered
            StreamReader bat = new StreamReader(@"C:\ProgramData\Autodesk\Revit\Addins\2013\RevitPlus\RevitPlusBatchTemplate.bat");
            String batchfile = bat.ReadToEnd();
            bat.Close(); // Must close it before proceeding
            bat.Dispose();// Must dispose it before proceeding

            
            //Tells energy Plus to read the idf file in it's directory
            batchfile = batchfile.Replace("%~1", @"C:\EnergyPlusV7-2-0\RevitPlusIDF");

            //Tells Energy Plus to store all results in results folder under the project name
            batchfile = batchfile.Replace("%~2", ResultsPath + "\\" + StoredVariables.ProjectName);

            //Sets weather file to be used by Energy Plus
            batchfile = batchfile.Replace("%~4", StoredVariables.WeatherFile);

            //Creates a new instance of the batchfile and overwrites batchfile
            StreamWriter newbatch = new StreamWriter(@"C:\EnergyPlusV7-2-0\Run.bat");
            newbatch.Write(batchfile);
            newbatch.Close();

            //Starts the batchfile
            System.Diagnostics.Process.Start(@"C:\EnergyPlusV7-2-0\Run.bat");
        }
    }
}